import pymysql
from pymysql import cursors


def get_connection():
    # 连接数据库
    conn = pymysql.connect(host='localhost',
                           user='root',
                           password='admin',
                           db='cov',
                           cursorclass=cursors.DictCursor,
                           charset='utf8')
    cursor = conn.cursor()
    return conn, cursor


def close_connection(conn, cursor):
    # 关闭数据库
    cursor.close()
    conn.close()


def findAll():
    # 查询所有数据
    conn, cursor = get_connection()
    sql = "SELECT * FROM lastupdate"
    cursor.execute(sql)
    result = cursor.fetchall()
    close_connection(conn, cursor)
    return result


def edit(data):
    # 编辑数据
    conn, cursor = get_connection()
    sql = "UPDATE lastupdate SET lastDate=%s, operateRole=%s WHERE id=%s"
    cursor.execute(sql, [data['update_time'], data['role'], data['id']])
    conn.commit()
    close_connection(conn, cursor)
